//
//  FHVisitorView.swift
//  WeiBo
//
//  Created by qfh on 16/7/9.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit



class FHVisitorView: UIView {

    /// 轮播图片视图
    lazy var cycleImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /// 遮罩图片视图
    lazy var maskImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /// 中间 home的图片的视图
    lazy var houseImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 点击登录 或者 注册执行的闭包
    var loginClosure: (() -> ())?
    /// 页面中的信息 label
    lazy var massgeLabel: UILabel = {
    
        let label = UILabel()
        
        label.textColor = UIColor.grayColor()
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        label.numberOfLines = 0
        
        label.textAlignment = .Center
        
        
        return label
        
    }()
    
    /// 注册button
    lazy var registButton: UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: .Normal)
        
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        
        button.setTitleColor(UIColor.redColor(), forState: .Highlighted)
        
        button.addTarget(self, action: "loginActino", forControlEvents: .TouchUpInside)
        
        
        return button
    
    }()
    
    /// 登录按钮
    lazy var loginButton: UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: .Normal)
        
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        
        button.setTitleColor(UIColor.redColor(), forState: .Highlighted)
        
        button.addTarget(self, action: "loginActino", forControlEvents: .TouchUpInside)
        
        return button
        
    }()
    
   // MARK: - 初始化
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        loadUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 加载UI页面
    
    
    /**
    添加 子视图
    */
    func loadUI() {
    
        addSubview(cycleImageView)
        
        addSubview(maskImageView)
        
        addSubview(houseImageView)
        
        addSubview(massgeLabel)
        
        addSubview(registButton)
        
        addSubview(loginButton)
        
        
        
        
    }
    
    /**
     加载自动布局
     */
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        cycleImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
            
        }
        
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        
        houseImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        
        massgeLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(cycleImageView)
            
            make.top.equalTo(cycleImageView.snp_bottom)
            
            make.width.equalTo(224)
        }
        
        registButton.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(massgeLabel)
            
            make.top.equalTo(massgeLabel.snp_bottom).offset(10)
            
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(massgeLabel)
            
            make.top.equalTo(registButton)
            
            make.size.equalTo(registButton)
            
        }
        
        
        
    }
    
    
   
    
    
    // MARK: - 注册或者登录点击 
    
    @objc private func loginActino() {
    
        loginClosure?()
    
    }

}


// MARK: - 扩展 视图更新

extension FHVisitorView {

    // MARK: - 更新view
    
    /**
    更新视图
    
    - parameter title:     显示的title
    - parameter imageName: 图片imageName
    */
    func updateVisitorView(title: String?, imageName: String?) {
        
        // 都写值了  说明不是首页视图  需要隐藏旋转图片

        if let masge = title, image = imageName {
            massgeLabel.text = masge
            
            houseImageView.image = UIImage(named: image)
            
            cycleImageView.hidden = true
            //  是首页
        }else {
            
            cycleImageView.hidden = false
            
            cycleAnimation()
        }
        
    }
    
    
    // MARK: - 加载旋转动画
    /**
    旋转图片的转场动画
    */
    func cycleAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.duration = 20;
        
        animation.repeatCount = MAXFLOAT
        
        animation.removedOnCompletion = false
        
        cycleImageView.layer.addAnimation(animation, forKey: nil)
        
    }




}